package com.tencent.liteav.trtccalling.zhiyu.vm;

import android.app.Application;
import android.util.Log;
import androidx.annotation.NonNull;
import com.tencent.liteav.trtccalling.TUICalling;
import com.tencent.liteav.trtccalling.zhiyu.CallActivity;
import com.tencent.liteav.trtccalling.zhiyu.callback.BooleanCallback;
import com.tencent.liteav.trtccalling.zhiyu.event.CallAnswerEvent;
import com.ysg.base.BaseViewModel;
import com.ysg.bus.RxBus;
import com.ysg.bus.RxSubscriptions;
import com.ysg.bus.event.SingleLiveEvent;
import com.ysg.http.BaseResponse;
import com.ysg.http.HttpUtils;
import com.ysg.http.callback.OnErrorResult;
import com.ysg.http.callback.OnSuccessResult;
import com.ysg.http.data.CommonRepository;
import com.ysg.http.data.entity.im.ChatBlackEntity;
import com.ysg.http.data.entity.im.ChatDiamondEnoughEntity;
import com.ysg.http.data.entity.user.UserResult;
import com.ysg.utils.YStringUtil;
import com.ysg.utils.YToastUtil;
import java.util.HashMap;
import java.util.Map;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

public class CallVM extends BaseViewModel<CommonRepository> {
    public String txUserIdSelf;//自己的腾讯id
    public String txUserIdOther;//对方的腾讯id
    public String sex;
    public ChatBlackEntity blackEntity;
    public UserResult otherUserEntity;


    public CallVM(@NonNull Application application, CommonRepository model) {
        super(application, model);
    }

    public UIChangeObservable uc = new UIChangeObservable();
    public class UIChangeObservable {
        public SingleLiveEvent onCallAnswerEvent = new SingleLiveEvent<>();
        public SingleLiveEvent onFeeErrorEvent = new SingleLiveEvent<>();
        public SingleLiveEvent onDiamondRechargeEvent = new SingleLiveEvent<>();
        public SingleLiveEvent onPriceEvent = new SingleLiveEvent<>();
    }


    public Disposable disposable;
    @Override
    public void registerRxBus() {
        super.registerRxBus();
        disposable = RxBus.getDefault().toObservable(CallAnswerEvent.class)
                .subscribe(new Consumer<CallAnswerEvent>() {
                    @Override
                    public void accept(CallAnswerEvent event){
                        if (event != null){
                            Log.e("通话被接听", "通话被接听");
                            uc.onCallAnswerEvent.call();
                        }
                    }
                });
    }

    @Override
    public void removeRxBus() {
        super.removeRxBus();
        RxSubscriptions.remove(disposable);
    }

    /**
     * 是否被拉黑
     */
    public void requestIsBlack(){
        Map<String, String> map = new HashMap<>();
        map.put("tengxuncode", txUserIdOther);
        HttpUtils.getInstance()
                .data(model.isBlack(map), this, new OnSuccessResult<ChatBlackEntity>() {
                    @Override
                    public void onSuccessResult(BaseResponse<ChatBlackEntity> response) {
                        blackEntity = response.getData();
                    }
                });
    }

    /**
     * 扣费：通话
     */
    public void requestVideoAudioFee(){
        Map<String, String> map = new HashMap<>();
        map.put("type", CallActivity.freeType);//扣费类型：2.音频通话；3.视频通话；4.语音速配；5.视频速配
        map.put("txCode", txUserIdOther);//为女号的腾讯id
        HttpUtils.getInstance()
                .data(model.videoAudioFee(map), this, new OnSuccessResult() {
                    @Override
                    public void onSuccessResult(BaseResponse response) {
//                        blackEntity = response.getData();
                    }
                }, new OnErrorResult() {
                    @Override
                    public void onErrorResult(int code, String msg) {
                        uc.onFeeErrorEvent.call();//如余额不足等等
                    }
                });
    }

    /**
     * 请求用户信息
     */
    public void requestUser(){
        Map<String, String> map = new HashMap<>();
        map.put("userid", txUserIdOther);

        HttpUtils.getInstance()
                .data(model.userInfo(map), this,
                        new OnSuccessResult<UserResult>() {
                            @Override
                            public void onSuccessResult(BaseResponse<UserResult> response) {
                                otherUserEntity = response.getData();
                                uc.onPriceEvent.call();
                            }
                        });
    }

    /**
     * 通话的钻石是否足够-----------2
     */
    public void requestVideoAudioIsEnough(TUICalling.Type callEnum, BooleanCallback callback){
        Map<String, String> map = new HashMap<>();
        map.put("tengxuncode", txUserIdOther);
        map.put("type", TUICalling.Type.AUDIO == callEnum? "1": "2");
        HttpUtils.getInstance()
                .data(model.videoAudioIsEnough(map), this, new OnSuccessResult<ChatDiamondEnoughEntity>() {
                    @Override
                    public void onSuccessResult(BaseResponse<ChatDiamondEnoughEntity> response) {
                        if ((TUICalling.Type.AUDIO == callEnum && YStringUtil.eq(0, response.getData().getIsEnoughVoice()))
                                || (TUICalling.Type.VIDEO == callEnum && YStringUtil.eq(0, response.getData().getIsEnoughVideo()))
                        ){
                            uc.onDiamondRechargeEvent.call();
                            callback.onBoolean(false);
                            return;
                        }
                        callback.onBoolean(true);
                    }
                });
    }

    /**
     * 是否为女号
     */
    public boolean isGirl(){
        if (YStringUtil.eq(2, sex)){
            return true;
        }
        return false;
    }

    /**
     * 是否免费
     */
    public boolean isFree(){
        if (blackEntity == null){
            YToastUtil.showShort("blackEntity为空");
            return false;
        }
        if (YStringUtil.eq(1, blackEntity.getFree())){
            return true;
        }
        return false;
    }


}
